package com.hongxun.design.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.hongxun.design.R;
import com.hongxun.design.bean.ProjectItemBean;
import com.hongxun.design.bean.photopassbean.SystemTypeBean;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.utils.TimeUtil;
import com.hongxun.design.utils.fastjson.FastJsonUtil;
import com.hongxun.design.utils.httputil.HttpStringCodeTableUtil;
import com.hongxun.design.utils.imageutil.UriUtil;

import java.util.List;

/**
 * Created by zerdoor_pc on 2016/5/30.
 */
public class ProjectNavigationAdapter extends BaseAdapter {
    Context context;
    List<ProjectItemBean> itemBeans;
    LayoutInflater inflater;
    List<SystemTypeBean> projectType, designStage, projectSchedule;

    public ProjectNavigationAdapter(Context context, List<ProjectItemBean> data) {
        this.context = context;
        this.itemBeans = data;
        inflater = LayoutInflater.from(context);
        projectType = FastJsonUtil.JsonToData_Code(HttpStringCodeTableUtil.projectType);//项目类型
        designStage = FastJsonUtil.JsonToData_Code(HttpStringCodeTableUtil.designStage);//设计阶段
        projectSchedule = FastJsonUtil.JsonToData_Code(HttpStringCodeTableUtil.projectSchedule);//项目类型
    }

    @Override
    public int getCount() {
        return itemBeans.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        Hollder hollder = null;
        ProjectItemBean bean = itemBeans.get(i);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.listview_item_projectlist, null);
            hollder = new Hollder(convertView);
            convertView.setTag(hollder);
        } else {
            hollder = (Hollder) convertView.getTag();
        }
        hollder.image.setImageURI(UriUtil.getUri(bean.getThumb()));
        hollder.name.setText(bean.getProject_name());
        hollder.time.setText(TimeUtil.time(bean.create_time + "000"));

        hollder.schedule.setText(getProjectType(bean.getProject_type()));
        hollder.statel.setText(getDesignStage(bean.getStage()));
        hollder.stater.setText(getProjectSchedule(bean.getCurrent_schedule()));
        if (bean.getIs_exp().equals(MString.code_zero)) {
            hollder.datatype.setVisibility(View.GONE);
        } else {
            hollder.datatype.setVisibility(View.VISIBLE);
        }

        return convertView;
    }

    private String getProjectType(String code) {
        if (projectType == null)
            projectType = FastJsonUtil.JsonToData_Code(HttpStringCodeTableUtil.projectType);//项目类型
        for (SystemTypeBean s : projectType) {
            if (s.getTypecode().equals(code)) {
                return s.getTypename();
            }
        }
        return "";
    }

    private String getDesignStage(String code) {
        if (designStage == null)
            designStage = FastJsonUtil.JsonToData_Code(HttpStringCodeTableUtil.designStage);//设计阶段
        for (SystemTypeBean s : designStage) {
            if (s.getTypecode().equals(code)) {
                return s.getTypename();
            }
        }
        return "";
    }

    private String getProjectSchedule(String code) {
        if (projectSchedule == null)
            projectSchedule = FastJsonUtil.JsonToData_Code(HttpStringCodeTableUtil.projectSchedule);//项目类型
        for (SystemTypeBean s : projectSchedule) {
            if (s.getTypecode().equals(code)) {
                return s.getTypename();
            }
        }
        return "";
    }

    public class Hollder {
        public RelativeLayout item;
        public SimpleDraweeView image;
        public TextView name, statel, stater, right, schedule, time, datatype;

        Hollder(View convertView) {
            item = (RelativeLayout) convertView.findViewById(R.id.projectlist_item);
            image = (SimpleDraweeView) convertView.findViewById(R.id.projectlist_image);
            name = (TextView) convertView.findViewById(R.id.projectlist_name);
            statel = (TextView) convertView.findViewById(R.id.projectlist_statel);
            stater = (TextView) convertView.findViewById(R.id.projectlist_stater);
            right = (TextView) convertView.findViewById(R.id.projectlist_right);
            schedule = (TextView) convertView.findViewById(R.id.projectlist_schedule);
            time = (TextView) convertView.findViewById(R.id.projectlist_time);
            datatype = (TextView) convertView.findViewById(R.id.projectlist_datatype);
            SetTypeface.set(right, IconSourse.RIGHTARROW);
        }
    }
}
